#!/usr/bin/sh
#Version 190203.1

echo "Spark 7162 Enigma2"

modprobe e2_proc

echo "Init frontpanel"
modprobe aotom paramDebug=0
modprobe simu_button
if [ -e /lib/modules/2.6.32.71_stm24_0217/extra/encrypt ]; then
   modprobe encrypt
fi

if [ -e /var/etc/.firstboot ]; then
   echo "Wait 1 min." > /dev/vfd
   echo "[rcS] WAIT 1 min"
   rm /var/etc/.firstboot
   sleep 1
   touch /dev/.devfsd
else
   echo "Load 10" > /dev/vfd
   echo "[rcS] LOAD 10"
fi

if [ -e /etc/init.d/mountvirtfs ]; then
   /etc/init.d/mountvirtfs start
fi
if [ -e /etc/init.d/portmap ]; then
   /etc/init.d/portmap start
fi
if [ -e /etc/init.d/autofs ]; then
   /etc/init.d/autofs start
fi

echo "[rcS] Starting DEBUG Interface"
if [ -e /etc/init.d/mountall ]; then
   /etc/init.d/mountall start
fi

echo "[rcS] Init stmfb (LOAD 9)"
echo "Load 9" > /dev/vfd
modprobe stmcore-display-sti7105
if grep -q "config.av.videoport=Scart" /etc/enigma2/settings; then
  modprobe stmfb display0=720x576i-32@50:15m:pal:rgb:rgb; #SCART
else
  modprobe stmfb display0=1280x720-32@50:16m:pal:rgb:rgb; #HDMI
fi

echo "[rcS] Loading modules"

echo "[rcS] Load audio firmware (LOAD 8)"
echo "Load 8" > /dev/vfd
dtsdown=`cat /etc/enigma2/settings | grep systemoptions.dtsdownmix | cut -d "=" -f2`
if [ ! $dtsdown ] && [ -e /boot/audio_dts.elf ]; then
   /usr/bin/ustslave /dev/st231-1 /boot/audio_dts.elf > /dev/null
else
   /usr/bin/ustslave /dev/st231-1 /boot/audio.elf > /dev/null
fi

echo "[rcS] Load video firmware (LOAD 7)"
echo "Load 7" > /dev/vfd
/usr/bin/ustslave /dev/st231-0 /boot/video.elf > /dev/null

echo "[rcS] Init embx (LOAD 6)"
echo "Load 6" > /dev/vfd
modprobe embxshell
modprobe embxmailbox mailbox0=0xfe211000:136:set2 mailbox1=0xfe212000:0
modprobe embxshm mailbox0=shm:0:7:0x60000000:0:256:256:0:512:0x40000000:0x10000000
modprobe mme_host transport0=shm

echo "[rcS] Init AVS (LOAD 5)"
echo "Load 5" > /dev/vfd
modprobe avs type=avs_pio

player=`cat /proc/stb/player/version | awk '{ print substr( $0, length($0) - 2, length($0) ) }'`
echo "[rcS] Init player $player (LOAD 4)"
echo "Load 4" > /dev/vfd

modprobe mmelog
modprobe ksound
modprobe p2div64
modprobe stm_v4l2
modprobe stmvout
modprobe stmvbi
modprobe pseudocard

tunertype=`cat /etc/enigma2/settings | grep systemoptions.tunertype | cut -d "=" -f2`
if [ ! $tunertype ]; then
   tunertype='t'
fi
modprobe spark7162 UnionTunerType=$tunertype

modprobe pti waitMS=20 videoMem=4096
if [ $? -ne 0 ]; then
   modprobe pti
   echo "[rcS] Simple pti"
else
   echo "[rcS] Full feature pti"
fi
modprobe stm_monitor
modprobe stmsysfs
modprobe stmdvb

echo "[rcS] LOAD 3"
echo "Load 3" > /dev/vfd
#modprobe player2 discardlateframe=0
modprobe player2
modprobe sth264pp
modprobe stmalloc
modprobe platform
modprobe silencegen

modprobe smartcard

if [ -e /lib/modules/2.6.32.71_stm24_0217/extra/dvb-it913x ]; then
  echo
  echo "[rcS] IT913X driver loading"
  echo "[rcS] ---------------------"
  modprobe dvb-pll
  modprobe dvb-it913x
  echo
  echo "[rcS] End of IT913X driver loading"
  echo "[rcS] ----------------------------"
fi

if [ -e /lib/modules/2.6.32.71_stm24_0217/extra/dvb-usb-rtl2832u ]; then
  echo
  echo "[rcS] RTL2832U driver loading"
  echo "[rcS] -----------------------"
  modprobe dvb-usb-rtl2832u
  echo
  echo "[rcS] End of RTL2832U driver loading"
  echo "[rcS] ------------------------------"
fi

if [ -e /lib/modules/2.6.32.71_stm24_0217/extra/usb_serial ]; then
  modprobe usb_serial
  if [ -e /lib/modules/2.6.32.71_stm24_0217/extra/option ]; then
    modprobe option
  fi
  if [ -e /lib/modules/2.6.32.71_stm24_0217/extra/usb_wwan ]; then # 3G modem
    modprobe usb_wwan
  fi
  modprobe option
fi

echo "[rcS] Init HDMI-CEC"
modprobe cec activemode=1

modprobe bpamem
mknod /dev/bpamem c 153 0

if [ -e /usr/share/bootlogo.mvi ]; then
   showiframe -p /usr/share/bootlogo.mvi &
fi
stfbcontrol a 0

echo "[rcS] Init lircd (LOAD 2)"
echo "Load 2" > /dev/vfd
/usr/bin/lircd
rm -rf /dev/rc
mknod /dev/rc c 147 1 #VFD
/usr/bin/evremote2 10 120 &

echo "[rcS] Init smartcard cardreader"
mknod /dev/sci0 c 169 0
mknod /dev/sci1 c 169 1

#major=$(awk "\$2==\"sparkid\" {print \$1}" /proc/devices)
#rm -f /dev/sparkid
#mknod /dev/sparkid c $major 0

stfbcontrol a 255

#wlan=`cat /etc/enigma2/settings | grep systemoptions.wlan | cut -d "=" -f2`
wlan=
if [ ! $wlan ] || [ ! -e /lib/modules/2.6.32.71_stm24_0217/extra/$wlan ]; then
   echo "[rcS] No WLAN"
else
   modprobe $wlan
   echo "[rcS] $wlan WLAN"
#   echo "$wlan WLAN" > /dev/vfd
#   sleep 3
fi

if [ -e /lib/modules/2.6.32.71_stm24_0217/extra/ntfs ]; then
   modprobe ntfs
fi

if [ -e /lib/modules/2.6.32.71_stm24_0217/extra/cifs ]; then
   modprobe cifs
fi

echo "[rcS] Init FTP (LOAD 1)"
echo "Load 1" > /dev/vfd
if [ -e /etc/init.d/vsftpd ]; then
   /etc/init.d/vsftpd start
fi
/usr/sbin/inetd

# Start automount
if [ -e /etc/init.d/automount ]; then
   python /etc/init.d/automount &
fi

# Do own extras
if [ -e /etc/ownscript.sh ]; then
   /etc/ownscript.sh &
fi

echo "[rcS] Start Enigma2"
echo "Start SH4 Enigma2" > /dev/vfd

until false
do
   echo "[rcS] Entering E2 main loop"
   if [ -e /usr/share/backdrop.mvi ]; then
      showiframe -p /usr/share/backdrop.mvi
   fi
   /usr/bin/enigma2
   rtv=$?
   echo "[rcS] E2 ended <- RTV: " $rtv
   /usr/bin/fp_control -i 46 0
   case "$rtv" in
      0)  echo "[rcS] E2 ERROR"
          echo "E2 Error" > /dev/vfd
          init 6;;
      1)  echo "[rcS] SHUTDOWN"
          echo "Shutdown" > /dev/vfd
          if [ -e /usr/share/shutdown.mvi ]; then
             showiframe -p /usr/share/shutdown.mvi
          fi
          init 0;;
      2)  echo "[rcS] REBOOT"
          echo "Reboot" > /dev/vfd
          init 6;;
      3)  echo "[rcS] E2 RESTART"
          echo "Restart GUI" > /dev/vfd
          echo "0" > /proc/progress
          if [ -e /usr/share/backdrop.mvi ]; then
             showiframe -p /usr/share/backdrop.mvi
          else
             showiframe -p /usr/share/bootlogo.mvi &
          fi;;
      10) echo "[rcS] SPARK RESTART"
          echo "SPARK Restart" > /dev/vfd
          modprobe i2s
          if [ ! -f /etc/fw_env.config ]; then
             echo "fw_env.config not found - will create it"
             echo '/dev/mtd7 0x000A0000 0x00020000 0x20000' > /etc/fw_env.config
          fi
          var=`fw_printenv | grep 'bootargs_spark='` ; fw_setenv bootargs ${var#*=}
          var=`fw_printenv | grep 'bootcmd_spark='` ; fw_setenv bootcmd ${var#*=}
          var=`fw_printenv | grep 'boot_system_spark='` ; fw_setenv boot_system ${var#*=}
          var=`fw_printenv | grep 'userfs_base_spark='` ; fw_setenv userfs_base ${var#*=}
          var=`fw_printenv | grep 'userfs_len_spark='` ; fw_setenv userfs_len ${var#*=}
          var=`fw_printenv | grep 'kernel_base_spark='` ; fw_setenv kernel_base ${var#*=}
          var=`fw_printenv | grep 'kernel_name_spark='` ; fw_setenv kernel_name ${var#*=}
          var=`fw_printenv | grep 'userfs_name_spark='` ; fw_setenv userfs_name ${var#*=} 
          var=`fw_printenv | grep 'loadu_userfs_spark='` ; fw_setenv loadu_userfs ${var#*=}
          var=`fw_printenv | grep 'loadu_kernel_spark='` ; fw_setenv loadu_kernel ${var#*=}
          var=`fw_printenv | grep 'kernel_len_spark='` ; fw_setenv kernel_len ${var#*=}
          sync
          init 6;;
      *)  echo "[rcS] ERROR"
          echo "Error" > /dev/vfd
          init 6;;
   esac
done
